package com.example.user.customviewdemo.drawerlayoutmenu;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.example.user.customviewdemo.R;

import java.lang.reflect.Method;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ContextMenuActivity extends AppCompatActivity {

    @Bind(R.id.textview1)
    TextView textview1;
    @Bind(R.id.textview2)
    TextView textview2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_context_menu);
        ButterKnife.bind(this);

        registerForContextMenu(textview1);
        registerForContextMenu(textview2);

    }


    @Override
    protected boolean onPrepareOptionsPanel(View view, Menu menu) {
        if (menu != null) {
            Log.v("tag","menu className: "+menu.getClass().getSimpleName());
            if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
                try{
                    Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    m.setAccessible(true);
                    m.invoke(menu, true);
                } catch (Exception e) {
                    Log.e(getClass().getSimpleName(), "onMenuOpened...unable to set icons for overflow menu", e);
                }
            }
        }
        return super.onPrepareOptionsPanel(view, menu);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        if(v == textview1){
            MenuItem item1 = menu.add(1,1,1,"textview1  menuitem1");
            item1.setIcon(R.mipmap.ic_launcher);
            menu.add(1,2,2,"textview1  menuitem2");
            menu.add(1,3,3,"textview1  menuitem3");
        }else if(v == textview2){
            /*menu.add(2,3,1,"textview2  menuitem1");
            menu.add(2,4,2,"textview2  menuitem2");
            menu.add(2,5,3,"textview2  menuitem3");*/
            getMenuInflater().inflate(R.menu.main_menu,menu);
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        return true;
       // return super.onContextItemSelected(item);
    }
}
